package afaas.framework.dal.mysql.impl;

import afaas.framework.dal.mysql.PropertyService;
import afaas.framework.util.LangUtil;
import afaas.framework.util.entity.ResultSupport;
import com.google.common.base.Preconditions;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

public class PropertyServiceImpl implements PropertyService {
	
	private Properties properties;

	private String fileName;

	public PropertyServiceImpl(String fileName){
		Preconditions.checkArgument(fileName != null && !"".equals(fileName.trim()));
		this.fileName = fileName;
	}

	public void init() throws Exception {

		FileInputStream fileInputStream = new FileInputStream(fileName);
		InputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
		
		properties = new Properties();
		properties.load(bufferedInputStream);

		bufferedInputStream.close();
		fileInputStream.close();

	}
	
	@Override
	public ResultSupport<String> get(String key) {
		
		ResultSupport<String> ret = new ResultSupport<String>();
		
		return ret.success(LangUtil.safeString(properties.get(key)));
		
	}
	
	public static void main(String[] args) throws Exception {
		
		PropertyServiceImpl p = new PropertyServiceImpl("");
		
		p.init();
		
		p.get("a");
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
